//
//  HJMineController.m
//  KaiChat
//
//  Created by lx on 2023/12/14.
//

#import "HJMineController.h"
#import "HJBackgroundView.h"
#import "HJIconTitleCell.h"
#import "HJUIAlertController.h"
#import "HJMinePresent.h"
#import "HJGradientView.h"
#import "HJIM.h"
#import "HJRecaptchaClient.h"
#import "HJButton.h"
#import "HJUserModel.h"
#import "HJIconTitleRedPointCell.h"
#import "HJWebViewController.h"
#import "HJFlutterManager.h"
#import "HJAlertController.h"
#import "HJTitleSubTitleView.h"
#import "HJDAppView.h"
#import "HJIconTitleCellButton.h"
#import "HJWalletView.h"
#import "HJFlutterController.h"
#import "HJInformationPresent.h"
#import "HJPostHomePageController.h"


@interface HJMineController ()

@property (nonatomic, assign) CGFloat margin;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) UILabel *nicknameLabel;
@property (nonatomic, weak) UILabel *UIDLabel;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, weak) UILabel *walletAddressLabel;
@property (nonatomic, weak) UIButton *walletAddressCopyButton;
@property (nonatomic, weak) UIImageView *topBackgroundImageView;
@property (nonatomic, assign) CGFloat tableViewH;
@property (nonatomic, weak) UIView *buttomView;
@property (nonatomic, strong) NSMutableDictionary *titleSubTitleViewPool;
@property (nonatomic, weak) HJDAppView *dAppView;
@property (nonatomic, weak) HJWalletView *walletView;

@end

@implementation HJMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _margin = 0;
    
    UIImageView *backgroundImageView = [UIImageView addToView:self.view];
    backgroundImageView.image = [UIImage imageNamed:@"mine_bg"];
    backgroundImageView.userInteractionEnabled = YES;
    backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    self.navigationBar.alpha = 0;
    
    HJBarButton *qrCodeButton = [HJBarButton barButtonWithImage:[UIImage imageNamed:@"qrcode_icon"] target:self action:@selector(didClickQrCodeButton)];
    HJBarButton *settingButton = [HJBarButton barButtonWithImage:[UIImage imageNamed:@"setting_icon"] target:self action:@selector(didClickSettingButton)];
    
    [self.navigationBar.contentView addSubview:settingButton];
    [settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(32);
        make.centerY.offset(0);
        make.right.inset(15);
    }];
    
    [self.navigationBar.contentView addSubview:qrCodeButton];
    [qrCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(32);
        make.centerY.offset(0);
        make.right.equalTo(settingButton.mas_left).inset(8);
    }];
    
    UIScrollView *scrollView = [UIScrollView addToView:backgroundImageView];
    scrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - TabBarHeight);
    CGSize contentSize = scrollView.bounds.size;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    scrollView.contentSize = contentSize;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.alwaysBounceVertical = YES;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    UIView *contentView = [[UIView alloc] init].addTo(scrollView);
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    UIView *infoView = [UIView addToView:contentView];
    [infoView addTapGesturesWithTarget:self action:@selector(tapInfoView)];
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(70);
        make.top.offset(KNavBarHight);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:infoView];
    avatarImageView.addCornerRadius(34);
    avatarImageView.layer.borderColor = UIColor.whiteColor.CGColor;
    avatarImageView.layer.borderWidth = 1;
    [avatarImageView tj_setImageWithURL:HJUserInfo.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.width.height.offset(68);
        make.centerY.offset(0);
    }];
    
    UILabel *nicknameLabel = [UILabel lableWithSuperView:infoView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:HJUserInfo.nickname textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView).offset(12);
        make.left.equalTo(avatarImageView.mas_right).offset(12);
        make.right.mas_lessThanOrEqualTo(-35);
    }];
    
    UIImageView *arrowImageView = [UIImageView addToView:infoView];
    arrowImageView.image = [UIImage imageNamed:@"me_arrow"];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel.mas_right).inset(5);
        make.centerY.equalTo(nicknameLabel);
    }];
    
    UILabel *walletAddressLabel = [UILabel lableWithSuperView:infoView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    walletAddressLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [walletAddressLabel addTapGesturesWithTarget:self action:@selector(didClickAddressCopyButton)];
    NSString *address = truncateString(HJUserInfo.MPCWalletAddress, 6, 6);
    walletAddressLabel.text = [NSString stringWithFormat:@"%@", address];
    [walletAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel);
        make.top.equalTo(nicknameLabel.mas_bottom).offset(6);
        make.width.mas_lessThanOrEqualTo(KScreenWidth - leftRightMargin * 2 - 100);
    }];
    
    UIButton *walletAddressCopyButton = [UIButton addToView:infoView];
    [walletAddressCopyButton addTarget:self action:@selector(didClickAddressCopyButton) forControlEvents:UIControlEventTouchUpInside];
    [walletAddressCopyButton setImage:[UIImage imageNamed:@"mine_copy_icon"] forState:UIControlStateNormal];
    walletAddressCopyButton.hidden = YES;
    [walletAddressCopyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(walletAddressLabel.mas_right);
        make.centerY.equalTo(walletAddressLabel);
        make.width.height.offset(20);
    }];
    
    UIView *newsInfoView = [self addNewsInfoView];
    [contentView addSubview:newsInfoView];
    [newsInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(infoView.mas_bottom).offset(20);
        make.left.right.inset(leftRightMargin);
        make.height.offset(40);
    }];
    
    UIImageView *integralBackgroundImageView = [UIImageView addToView:contentView];
    integralBackgroundImageView.image = [UIImage imageNamed:@"mine_integral_bg"];
    [integralBackgroundImageView addTapGesturesWithTarget:self action:@selector(tapIntegralBackgroundImageView)];
    [integralBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(newsInfoView.mas_bottom).offset(16);
        make.left.right.inset(leftRightMargin);
        make.height.offset(ZOOM(50));
    }];
    
    HJWalletView *walletView = [HJWalletView addToView:contentView];
    walletView.backgroundColor = UIColor.whiteColor;
    walletView.addCornerRadius(10);
    walletView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
    walletView.layer.borderWidth = 0.5;
    [walletView addTapGesturesWithTarget:self action:@selector(tapWalletView)];
    [walletView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(integralBackgroundImageView.mas_bottom).offset(15);
        make.left.right.inset(leftRightMargin);
        make.height.offset(HZoom(80));
    }];
    _walletView = walletView;
    
    UIView *listView = [self addListView];
    [contentView addSubview:listView];
    
    if (isTestFight()) {
        HJDAppView *dAppView = [HJDAppView addToView:contentView];
        dAppView.backgroundColor = UIColor.whiteColor;
        dAppView.addCornerRadius(10);
        dAppView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
        dAppView.layer.borderWidth = 0.5;
        TJWeakify(self)
        [dAppView setDidSelectItemAtIndexPathBlock:^(NSDictionary *  _Nonnull dict) {
            [weakself didSelectDAppItemAtIndexPath:dict];
        }];
        [dAppView setDidClickMoreBlock:^{
            [weakself didSelectDAppItemAtIndexPath:nil];
        }];
        [dAppView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(walletView.mas_bottom).offset(15);
            make.left.right.inset(leftRightMargin);
            make.height.offset(HZoom(124));
        }];
        
        [listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(dAppView.mas_bottom).offset(15);
            make.left.right.inset(leftRightMargin);
            make.height.offset(HZoom(100));
        }];
        
        _dAppView = dAppView;
    } else {
        
        [listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(walletView.mas_bottom).offset(15);
            make.left.right.inset(leftRightMargin);
            make.height.offset(HZoom(100));
        }];
    }
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
    contentSize.height = CGRectGetMaxY(listView.frame) + 15;
    scrollView.contentSize = contentSize;
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(contentSize.height);
    }];
    
    _avatarImageView = avatarImageView;
    _nicknameLabel = nicknameLabel;
    _walletAddressCopyButton = walletAddressCopyButton;
    _walletAddressLabel = walletAddressLabel;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleModifyAvatarSucceedNotification:) name:modifyAvatarSucceedNotification object:nil];
    [self handleUserData:HJTools.tool.userModel];
    
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        dispatch_async_main_queue(^{
            walletView.dict = userModel.walletInfo;
            [self updateAddressWithWalletInfo:userModel.walletInfo];
            [self refreshStatisticsDataWithDict: @{
                @"posts": @(userModel.posts),
                @"fans": @(userModel.fans),
                @"follows": @(userModel.follows),
                @"friends": @(userModel.friends),
            }];
        });
    });
    
    if (isTestFight()) {
        [HJMinePresent loadDappBookmarkListComplete:^(NSArray * _Nonnull array) {
            self.dAppView.datas = array;
        }];
    }
}

- (UIView *)addNewsInfoView {
    UIView *contentView = [[UIView alloc] init];
    for (int i = 0; i < 4; i++) {
        CGFloat W = (KScreenWidth - leftRightMargin * 2) / 4;
        HJTitleSubTitleView *titleSubTitleView = [[HJTitleSubTitleView alloc] init];
        titleSubTitleView.frame = CGRectMake(i * W, 0, W, 40);
        titleSubTitleView.anyObject = @(i);
        [contentView addSubview:titleSubTitleView];
        [self.titleSubTitleViewPool setObject:titleSubTitleView forKey:@(i)];
        [titleSubTitleView addTapGesturesWithTarget:self action:@selector(tapTitleSubTitleView:)];
    }
    
    
    HJTitleSubTitleView *friendsView = self.titleSubTitleViewPool[@0];
    friendsView.titleLabel.text = @"0";
    friendsView.subTitleLabel.text = @"Friends";
    
    HJTitleSubTitleView *followView = self.titleSubTitleViewPool[@1];
    followView.titleLabel.text = @"0";
    followView.subTitleLabel.text = @"Follow";
    
    HJTitleSubTitleView *fansView = self.titleSubTitleViewPool[@2];
    fansView.titleLabel.text = @"0";
    fansView.subTitleLabel.text = @"Fans";
    
    HJTitleSubTitleView *postView = self.titleSubTitleViewPool[@3];
    postView.titleLabel.text = @"0";
    postView.subTitleLabel.text = @"Post";
    
    return contentView;
}

- (void)refreshStatisticsDataWithDict:(NSDictionary *)dict {
        
    HJTitleSubTitleView *friendsView = self.titleSubTitleViewPool[@0];
    friendsView.titleLabel.text = toString(dict[@"friends"]);
    
    HJTitleSubTitleView *followView = self.titleSubTitleViewPool[@1];
    followView.titleLabel.text = toString(dict[@"follows"]);
    
    HJTitleSubTitleView *fansView = self.titleSubTitleViewPool[@2];
    fansView.titleLabel.text = toString(dict[@"fans"]);
    
    HJTitleSubTitleView *postView = self.titleSubTitleViewPool[@3];
    postView.titleLabel.text = toString(dict[@"posts"]);
}

- (void)tapTitleSubTitleView:(UITapGestureRecognizer *)tap {
    
    HJTitleSubTitleView *titleSubTitleView = (HJTitleSubTitleView *)tap.view;
    
    NSInteger index = [titleSubTitleView.anyObject intValue];
    
    if (index == 0) {
        [getMainController() tj_setSelectedIndex:1];
        return;
    }
    
    if (index == 1) {
        HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageFollow];
        flutterController.arguments = @{
            @"username": HJUserInfo.nickname,
            @"userId": HJUserInfo.userId
        };
        [self.navigationController pushViewController:flutterController];
        return;
    }
    
    if (index == 2) {
        HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageFans];
        flutterController.arguments = @{
            @"username": HJUserInfo.nickname,
            @"userId": HJUserInfo.userId
        };
        [self.navigationController pushViewController:flutterController];
        return;
    }
    
    HJPostHomePageController *postHomePageController = [[HJPostHomePageController alloc] init];
    postHomePageController.userId = HJUserInfo.userId;
    [self.navigationController pushViewController:postHomePageController];
    
}

- (UIView *)addListView {
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.addCornerRadius(10);
    contentView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
    contentView.layer.borderWidth = 0.5;
    
    HJIconTitleCellButton *helpView = [HJIconTitleCellButton addToView:contentView];
    [helpView addTarget:self action:@selector(didClickCellButton:) forControlEvents:UIControlEventTouchUpInside];
    helpView.model = [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
        model.iconName = @"mine_help_icon";
        model.text = @"Help";
        model.identifier = @"help";
        model.showArrow = YES;
        model.showLine = YES;
    }];
    
    [helpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(HZoom(50));
    }];
    
    HJIconTitleCellButton *aboutView = [HJIconTitleCellButton addToView:contentView];
    [aboutView addTarget:self action:@selector(didClickCellButton:) forControlEvents:UIControlEventTouchUpInside];
    aboutView.model = [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
        model.iconName = @"mine_about_icon";
        model.identifier = @"about";
        model.text = @"About Kai";
        model.controllerName = @"HJAboutController";
        model.showArrow = YES;
    }];
    
    [aboutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(helpView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(HZoom(50));
    }];
    return contentView;
}

- (void)tapInfoView {
    [self.navigationController pushViewControllerWithName:@"HJPersonalCenterV2Controller"];
}

- (void)didSelectDAppItemAtIndexPath:(NSDictionary *)dict {
    HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageDApp];
    if (dict) {
        flutterController.arguments = @{@"url": dict[@"url"]};
    }
    [self.navigationController pushViewController:flutterController];
}

- (void)didClickCellButton:(HJIconTitleCellButton *)button {
    
    HJCellModel *model = button.model;
    
    if ([model.identifier isEqualToString:@"help"]) {
        HJWebViewController *webViewController = [[HJWebViewController alloc] init];
        webViewController.type = HJWebViewTypeHelp;
        webViewController.url = @"https://kaichat.io/m/help-center/kaiearn";
        [self.navigationController pushViewController:webViewController];
        return;
    }
    
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    
    [self.navigationController pushViewControllerWithName:controllerName];
}

- (void)tapWalletView {
    HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageWallet];
    [self.navigationController pushViewController:flutterController];
}

- (void)tapIntegralBackgroundImageView {
    HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageIntegral];
    [self.navigationController pushViewController:flutterController];
}

- (void)resetBottomDataWithDatas:(NSArray *)outsideChainDatas {
    
    CGFloat margin = 14;
    CGFloat outsideChainButtonH = 34;
    CGFloat totalW = outsideChainDatas.count * outsideChainButtonH + (outsideChainDatas.count - 1) * margin;
    
    UIView *outsideChainView = [UIView addToView:self.buttomView];
    [outsideChainDatas enumerateObjectsUsingBlock:^(HJCellModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HJButton *button = [HJButton addToView:outsideChainView];
        button.anyObject = obj;
        [button setImage:[UIImage imageNamed:obj.iconName] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset((outsideChainButtonH + margin) * idx);
            make.width.height.offset(outsideChainButtonH);
            make.centerY.offset(0);
        }];
    }];
    
    [outsideChainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(totalW);
        make.centerX.offset(0);
        make.top.inset(0);
        make.height.offset(34);
    }];
    
    UILabel *versionLabel = [UILabel lableWithSuperView:self.buttomView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:[NSString stringWithFormat:@"Kai V%@", getAppVersion()] textAlignment:NSTextAlignmentLeft];
    [versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(outsideChainView.mas_bottom).offset(12);
    }];
    
}

- (void)handleDidClickBindingGoogleOrAppleNotification {
    self -> _datas = nil;
    [self.tableView reloadData];
}

- (void)handleModifyAvatarSucceedNotification:(NSNotification *)not {
    UIImage *image = not.object;
    if (image) {
        _avatarImageView.image = image;
    }
}

- (void)didClickButton:(HJButton *)button {
    HJCellModel *obj = button.anyObject;
    
    NSString *urlString = obj.detailText.encodeUrl;
    NSURL *URL = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:URL]) {
        [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:^(BOOL success) {
            if (!success) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:obj.textValue] options:@{} completionHandler:nil];
            }
        }];
    }
}

- (void)didClickUIDCopyButton {
    [HJTools copyStringToPasteboard:HJUserInfo.no];
    [HJHUD showSucceedMessage:@"Copied Success"];
}

- (void)didClickAddressCopyButton {
    [HJTools copyStringToPasteboard:HJUserInfo.MPCWalletAddress];
    [HJHUD showSucceedMessage:@"Copied Success"];
}

- (void)didClickQrCodeButton {
    [self.navigationController pushViewControllerWithName:@"HJQrCodeController"];
}

- (void)didClickSettingButton {
    [self.navigationController pushViewControllerWithName:@"HJSettingController"];
}


- (void)tapTopViewContentView {
    [self.navigationController pushViewControllerWithName:@"HJPersonalCenterController"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.row];
    HJIconTitleCell *cell = nil;
    if ([model.identifier isEqualToString:@"setting"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"HJIconTitleRedPointCell" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"HJIconTextCell" forIndexPath:indexPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    
    if ([model.identifier isEqualToString:@"help"]) {
        HJWebViewController *webViewController = [[HJWebViewController alloc] init];
        webViewController.type = HJWebViewTypeHelp;
        webViewController.url = @"https://kaichat.io/m/help-center/kaiearn";
        [self.navigationController pushViewController:webViewController];
        return;
    }
    
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    
    [self.navigationController pushViewControllerWithName:controllerName];
}

- (void)loadData {
    [HJMinePresent loadUserInfoComplete:^(HJUserModel * _Nullable userModel) {
        
        if (isEmptyString(userModel.nickname)) {
            return;
        }
        
        if (!([userModel.nickname isEqualToString:HJTools.tool.userModel.nickname] && [userModel.avatar isEqualToString:HJTools.tool.userModel.avatar])) {
            [[NSNotificationCenter defaultCenter] postNotificationName:userInfoChangeNotification object:HJTools.tool.userModel.userId];
        }
        
        [self.avatarImageView tj_setImageWithURL:userModel.avatar.URL placeholderImage:[UIImage imageNamed:@"avatar"]];
        self.nicknameLabel.text = userModel.nickname;
        self.UIDLabel.text = [NSString stringWithFormat:@"UID: %@", userModel.uid];
    }];
    
    TJWeakify(self)
    [HJFlutterManager.sharedManager getWalletInfoComplete:^(NSDictionary *walletInfo) {
        dispatch_async_main_queue(^{
            weakself.walletView.dict = walletInfo;
            [weakself updateAddressWithWalletInfo:walletInfo];
        });
    }];
    
    [HJInformationPresent loadUserHomePageDataWithUserId:HJUserInfo.userId complete:^(HJChatUserModel * _Nullable userModel) {
        [self refreshStatisticsDataWithDict:@{
            @"posts": @(userModel.posts),
            @"fans": @(userModel.fans),
            @"follows": @(userModel.follows),
            @"friends": @(userModel.friends),
        }];
    }];
    
    if (isTestFight() && self.dAppView.datas.count == 0) {
        [HJMinePresent loadDappBookmarkListComplete:^(NSArray * _Nonnull array) {
            self.dAppView.datas = array;
        }];
    }
}

- (void)updateAddressWithWalletInfo:(NSDictionary *)walletInfo {
    if (walletInfo && !isEmptyString(walletInfo[@"address"])) {
        NSString *address = truncateString(walletInfo[@"address"], 6, 6);
        self.walletAddressLabel.text = [NSString stringWithFormat:@"%@", address];
        self.walletAddressLabel.hidden = NO;
        self.walletAddressCopyButton.hidden = NO;
    } else {
        self.walletAddressLabel.hidden = YES;
        self.walletAddressCopyButton.hidden = YES;
    }
}

- (void)handleUserData:(HJUserModel *)model {
    [[SDWebImageManager sharedManager] loadImageWithURL:model.avatar.URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (image) {
            self.avatarImageView.image = image;
        }
    }];
    
    self.nicknameLabel.text = model.nickname;
    self.UIDLabel.text = [NSString stringWithFormat:@"UID: %@", model.uid];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
    
}

- (NSMutableDictionary *)titleSubTitleViewPool {
    if (!_titleSubTitleViewPool) {
        _titleSubTitleViewPool = [NSMutableDictionary dictionary];
    }
    return _titleSubTitleViewPool;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    TJLog(@"%s", __func__);
}

@end


